import { useRequest } from "ahooks";
import { evaluationV1ControllerBatch } from "@/services/auto/evaluation";
import { Fragment, useMemo } from "react";
import DataViewer from "common/component/DataViewer";
import { useModel } from "@umijs/max";
import { Skeleton } from "antd";
import type {DataViewerProps} from 'common/component/DataViewer/interface'

export default function ComposeChart({
   evaluationUuids,
   evaluationComposeUuids,
   eqUuids,
   projectUuids,
}: {
  evaluationUuids?: string[];
  evaluationComposeUuids?: string[];
  eqUuids?: string[];
  projectUuids?: string[];
}){
  // const {data: globalConfigData} = useModel('global-config', ({data})=>({data}));

  const {data: evaluationData, loading: evaluationDataLoading} = useRequest(()=>{
    return evaluationV1ControllerBatch({
      evaluationUuids: evaluationUuids,
      evaluationComposeUuids: evaluationComposeUuids,
      eqUuids: eqUuids,
    })
  }, {
    ready: !!(evaluationUuids?.length || evaluationComposeUuids?.length || eqUuids?.length),
    refreshDeps: [evaluationUuids?.join(','), evaluationComposeUuids?.join(','), eqUuids?.join(',')]
  });

  const chartPropsList: DataViewerProps[] | undefined = useMemo(()=>{
    if(!evaluationData){
      return undefined;
    }

    const handle = (data: API.EvaluationBatchResult[])=>{
      return data?.map(projectItem=>{
        const datas: DataViewerProps['datas'] = [];
        projectItem.list.forEach(listItem=>{
          datas.push(...listItem.datas.map(dataItem=>{
            return {
              ...dataItem,
              name: dataItem.name ? `${listItem.title} : ${dataItem.name}` : listItem.title,
            }
          }))
        })

        return {
          project: projectItem.project,
          datas,
          // targets: globalConfigData.targets,
        }
      })?.filter(item=>!projectUuids || projectUuids.includes(item.project.uuid))
        .sort((a,b)=>Number(!!b.datas.length) - Number(!!a.datas.length))
    }

    return handle(evaluationData)
  },[evaluationData]);

  if(!chartPropsList || evaluationDataLoading){
    return <Skeleton paragraph={{rows: 10}} />
  }

  return <Fragment>
    {
      chartPropsList?.map(item=>{
        return <DataViewer
          key={item.project.uuid}
          {...item}
        />
     })
    }
  </Fragment>
}
